package com.sevenStar.healthManagement.controller;

import com.sevenStar.healthManagement.entity.Admin;
import com.sevenStar.healthManagement.entity.User;
import com.sevenStar.healthManagement.service.AdminService;
import com.sevenStar.healthManagement.service.UserService;
import com.sevenStar.healthManagement.utils.VerifyUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/admins")
public class AdminController {
	@Autowired
	private AdminService adminService;
	@Autowired
	private UserService userService;

	@GetMapping("/login")
	public String login() {
		return "adminLogin";
	}

	@GetMapping("/logout")
	public String logout(HttpSession session) {
		session.invalidate();
		return "redirect:/admins/login";
	}

	@GetMapping("/index")
	public ModelAndView index(HttpSession session) {
		ModelAndView modelAndView = new ModelAndView();
		if (VerifyUtil.verifyLogin(session)) {
			modelAndView.addObject("users",adminService.getAllUser());
			modelAndView.setViewName("index");
		} else {
			modelAndView.setViewName("redirect:/admins/login");
		}
		return modelAndView;
	}

	@GetMapping("/addUser")
	public String addUser(HttpSession session) {
		if(VerifyUtil.verifyLogin(session)){
			return "addUser";
		}
		return "redirect:/admins/login";
	}

	@PostMapping("/login")
	public ModelAndView login(HttpSession session, Admin admin) {
		ModelAndView modelAndView = new ModelAndView();
		Admin a = adminService.login(admin);
		if (a != null) {
			session.setAttribute("username", admin.getUsername());
			session.setMaxInactiveInterval(3600);
			modelAndView.setViewName("redirect:/admins/index");
		} else {
			modelAndView.addObject("errorMsg","账号或密码错误!");
			modelAndView.setViewName("adminLogin");
		}
		return modelAndView;
	}

	@PostMapping("/addUser")
	public ModelAndView addUser(User user) {
		System.out.println(user);
		ModelAndView modelAndView = new ModelAndView();
		if (userService.queryPhoneOrIdNumber(user)) {
			modelAndView.addObject("errorMsg", "用户已存在!");
		} else {
			if (userService.register(user)) {
				modelAndView.addObject("successMsg", "添加成功!");
			} else {
				modelAndView.addObject("errorMsg", "添加失败!");
			}
		}
		modelAndView.setViewName("addUser");
		return modelAndView;
	}
	@GetMapping("/update")
	public ModelAndView update(String id, HttpSession session){
		ModelAndView modelAndView=new ModelAndView();
		if (VerifyUtil.verifyLogin(session)) {
			modelAndView.addObject("u",adminService.queryById(id));
			modelAndView.setViewName("modify");
		} else {
			modelAndView.setViewName("redirect:/admins/login");
		}

		return  modelAndView;
	}
	@PostMapping("/update")
	public ModelAndView update(User user){
		ModelAndView modelAndView = new ModelAndView();
		if(adminService.updateUser(user)){
			modelAndView.addObject("u", user);
			modelAndView.addObject("successMsg", "修改成功！");
			modelAndView.setViewName("modify");
		} else {
			modelAndView.addObject("errorMsg", "修改失败！");
			modelAndView.setViewName("modify");
		}

		return  modelAndView;
	}

	@RequestMapping("/delete")
	public ModelAndView delete(String id){
		ModelAndView modelAndView=new ModelAndView();
		userService.deleteUser(id);
		modelAndView.addObject("users",adminService.getAllUser());
		modelAndView.setViewName("index");
		return modelAndView;
	}
}
